package com.easyCms.base.web;

import com.easyCms.base.busi.RegisterUserBusi;
import com.easyCms.base.busi.vo.AppLoginRequestVO;
import com.easyCms.base.busi.vo.CreateAdminInfoVO;
import com.easyCms.base.busi.vo.CrmUserInfoVO;
import com.easyCms.base.busi.vo.RegisterUserInfoVO;
import com.easyCms.base.common.CodeConst;
import com.easyCms.base.common.RespCode;
import com.easyCms.base.entity.BsSystemInfo;
import com.easyCms.base.entity.ResultBean;
import com.easyCms.base.service.BsSystemInfoSV;
import com.easyCms.base.utils.ControllerUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

@Controller
@RequestMapping(value = "/loginData")
public class LoginDataController {
    private static Logger logger = LoggerFactory.getLogger(LoginDataController.class);
    @Autowired
    private RegisterUserBusi registerUserBusi;

    @Autowired
    private BsSystemInfoSV bsSystemInfoSV;

    /**
     * 用户登录
     *
     * @param appLoginRequestVO
     * @param request
     * @return
     */
    @RequestMapping(value = "/loginUser", method = RequestMethod.POST)
    public @ResponseBody
    CrmUserInfoVO loginUser(@RequestBody AppLoginRequestVO appLoginRequestVO,
                            HttpServletRequest request) {
        CrmUserInfoVO response = new CrmUserInfoVO();
        try {
            if (StringUtils.isBlank(appLoginRequestVO.getLoginName())
                    || StringUtils.isBlank(appLoginRequestVO.getPassword())) {
                response.setCode(RespCode.User_Register.INFO_NOTNULL_ERROR);
                response.setMessage(RespCode.User_Register.INFO_NOTNULL_ERROR_MESSAGE);
                return response;
            }

            response = registerUserBusi.appLogin(response, appLoginRequestVO, request);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return response;
    }

    /**
     * 发送短信校验码
     *
     * @param phoneNumber
     * @param checkCodeType
     * @return
     */
    @RequestMapping(value = "/sendSmsCheckCode", method = RequestMethod.GET)
    public @ResponseBody
    ResultBean sendSmsCheckCode(@RequestParam(value = "phoneNumber") String phoneNumber,
                                @RequestParam(value = "checkCodeType") Integer checkCodeType) {
        ResultBean result = new ResultBean();
        try {
            if (StringUtils.isEmpty(phoneNumber) || checkCodeType == null) {
                result.setCode(RespCode.User_Register.INFO_NOTNULL_ERROR);
                result.setCode(RespCode.User_Register.INFO_NOTNULL_ERROR_MESSAGE);
                return result;
            }
            return registerUserBusi.sendSmsCheckCode(result, phoneNumber, checkCodeType);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return result;
    }

    /**
     * 用户注册
     *
     * @param registerUserInfoVO
     * @param request
     * @return
     */
    @RequestMapping(value = "/registerUser", method = RequestMethod.POST)
    public @ResponseBody
    CrmUserInfoVO registerUser(@RequestBody RegisterUserInfoVO registerUserInfoVO,
                               HttpServletRequest request) {
        CrmUserInfoVO response = new CrmUserInfoVO();
        try {
            if (StringUtils.isBlank(registerUserInfoVO.getPassword())
                    || StringUtils.isBlank(registerUserInfoVO.getSmsCheckCode())
                    || null == registerUserInfoVO.getCheckCodeType()
                    || StringUtils.isBlank(registerUserInfoVO.getPhoneNumber())
                    || StringUtils.isBlank(registerUserInfoVO.getUserName())) {
                response.setCode(RespCode.User_Register.INFO_NOTNULL_ERROR);
                response.setMessage(RespCode.User_Register.INFO_NOTNULL_ERROR_MESSAGE);
                return response;
            }
            String userName = registerUserInfoVO.getUserName().trim();// 用户名
            String phoneNumber = registerUserInfoVO.getPhoneNumber().trim();// 电话号码
            String loginPassword = registerUserInfoVO.getPassword().trim(); // 登陆密码
            String smsCheckCode = registerUserInfoVO.getSmsCheckCode().trim();// 验证码
            Integer smsCheckCodeType = registerUserInfoVO.getCheckCodeType();// 验证码类型

            response = registerUserBusi.checkAndSaveUserRegisterData(response, loginPassword, userName, phoneNumber,
                    smsCheckCode, smsCheckCodeType, request);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return response;
    }

    /**
     * 用户退出
     *
     * @param request
     * @param printWriter
     * @return
     */
    @RequestMapping(value = "/userExit", method = RequestMethod.GET)
    public String userExit(HttpServletRequest request, PrintWriter printWriter) {
        try {
            registerUserBusi.deleteUserSession(request);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return "login/loginPage";
    }

    /**
     * 用户退出
     *
     * @param request
     * @param sessionName
     * @param printWriter
     * @return
     */
    @RequestMapping(value = "/querySession", method = RequestMethod.GET)
    public ResultBean querySession(HttpServletRequest request, String sessionName, PrintWriter printWriter) {
        ResultBean result = new ResultBean();
        try {
            result = registerUserBusi.querySession(sessionName, request, result);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return result;
    }

    /**
     * registerUserInfoVO
     *
     * @param createAdminInfoVO
     * @param request
     * @return
     */
    @RequestMapping(value = "/createAdminUser", method = RequestMethod.POST)
    public @ResponseBody
    ResultBean registerUser(@RequestBody CreateAdminInfoVO createAdminInfoVO, HttpServletRequest request) {
        ResultBean response = new ResultBean();
        try {
            if (StringUtils.isBlank(createAdminInfoVO.getPassword())
                    || StringUtils.isBlank(createAdminInfoVO.getPhoneNumber())
                    || StringUtils.isBlank(createAdminInfoVO.getUserName())) {
                response.setCode(RespCode.User_Register.INFO_NOTNULL_ERROR);
                response.setMsg(RespCode.User_Register.INFO_NOTNULL_ERROR_MESSAGE);
                return response;
            }
            String userName = createAdminInfoVO.getUserName().trim();// 用户名
            String phoneNumber = createAdminInfoVO.getPhoneNumber().trim();// 电话号码
            String loginPassword = createAdminInfoVO.getPassword().trim(); // 登陆密码

            response = registerUserBusi.createAdminUser(response, loginPassword, userName, phoneNumber, request);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return response;
    }


}
